<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2019/1/2
 * Time: 9:02
 */

namespace app\sports_admin\model;


use think\Model;
use think\Request;

class Notic extends Model
{
    protected $table = "notic";
    protected $pk    = "id";
    public function getNoticCat()
    {
        return $this->belongsTo("NoticCat","notic_cat","id")->field("name,id");
    }
    public function getSchool()
    {
        return $this->belongsTo("School","school_id","school_id")->field("school_id,school_name");
    }
    public function getTeacher()
    {
        return $this->belongsTo("Teacher","teacher_id","id")->field("id,teacher_name");
    }
    //教研文件列表
    public static function noticList($page,$rows,$sort,$sortOrder,$searchName,$searchType,$school)
    {
        $adminList = self::with('getNoticCat,getSchool')
            ->where(function($query)use($searchName,$searchType,$school) {
                    if($searchName) {
                        $query->where('title','like',"%{$searchName}%");
                    }
                    if($searchType){
                        $query->where('notic_cat',$searchType);
                    }
                    if($school){
                        $query->where('school_id',$school);
                    }
                }
            )
            ->page($page)
            ->limit($rows)
            ->order("{$sort}","{$sortOrder}")->select();

        $adminCount = self::with('getNoticCat,getSchool')
            ->where(function($query)use($searchName,$searchType,$school) {
                if($searchName) {
                    $query->where('title','like',"%{$searchName}%");
                }
                if($searchType){
                    $query->where('notic_cat',$searchType);
                }
                if($school){
                    $query->where('school_id',$school);
                }
            }
            )->count();
        return ['rows'=>$adminList,'total'=>$adminCount];
    }
    //删除教研文件
    public static function noticDel()
    {
        $data = Request::instance()->only("id");
        return self::where("id",$data['id'])->delete();
    }
    //校验文件信息
    public static function message()
    {
        $id = Request::instance()->only('id');
        return self::with('getTeacher')->where('id',$id['id'])->find();
    }
    //更新教研文件信息
    public static function noticUpdate()
    {
        $data = Request::instance()->only("id,title,content");
        $content = htmlspecialchars($data['content']);
        $time = time();
        return self::where("id",$data['id'])->update(['title'=>$data['title'],'content'=>$content,'update_time'=>$time]);
    }
    //添加教研文件
    public static function noticAdd()
    {
        $data = Request::instance()->only("title,content,notic_cat");
        $content = htmlspecialchars($data['content']);
        $time = time();
        return self::insert(['notic_cat'=>$data['notic_cat'],'content'=>$content,'title'=>$data['title']]);
    }

}